package com.btr.ivle;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.btr.ivle.R;
import com.btr.ivle.data.IVLEToken;
import com.btr.ivle.helper.DatabaseEvent;

public class ViewEventActivity extends SherlockActivity{
	
	public void onCreate(Bundle savedInstanceState) {
		setTheme(R.style.Theme_Sherlock_Light);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.post);
		
		Intent intent = getIntent();
	    String ID = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
	    boolean isEvent = intent.getBooleanExtra(MainActivity.MSG_ISEVENT, false);
	    String text = "";
	    
	    ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setDisplayShowHomeEnabled(false);
        
        TextView description = (TextView) findViewById(R.id.postDescription);
		TextView title = (TextView) findViewById(R.id.postTitle);
		
		DatabaseEvent dbEvent = new DatabaseEvent(this,IVLEToken.getUserID()+"_Event",null,getResources().getInteger(R.integer.DBVersion));
        String table = IVLEToken.getUserID()+"_Event"; 
        String[] columns = new String[] {dbEvent.Title, dbEvent.Description, dbEvent.EventDateTime, 
        		dbEvent.Organizer, dbEvent.Venue, dbEvent.Price, dbEvent.Agenda, dbEvent.Contact};
        String selection = dbEvent.ID + "=?";
		String[] selectionArgs = new  String[] {ID};
        Cursor c = dbEvent.getReadableDatabase().query(table, columns, 
        		selection, selectionArgs, null, null, null, null);
        c.moveToFirst();
        
        title.setText(c.getString(0));
        if(isEvent){
        	ab.setTitle("Student Event:");
        	text = "<p><font color=\"#808080\">by " + c.getString(3) + ",<br />" +
				c.getString(2) + "<br /></font></p>" + c.getString(1) + 
				"<br />Venue: <strong>" + c.getString(4) + "</strong><br />Price: <strong>" + c.getString(5) + 
				"</strong><br />Agenda: <strong>" + c.getString(6) + "</strong><br />Contact: <strong>" + c.getString(7) + "</strong>";
        }else{
        	ab.setTitle("IVLE News:");
        	Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(Long.parseLong(c.getString(2)));
            Date date = calendar.getTime();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy EEE hh:mma");
        	text = "<p><font color=\"#808080\">" + dateFormat.format(date) + "</font></p>" + c.getString(1);
        }
		description.setText(Html.fromHtml(text));
        
		
		c.close();
		dbEvent.getReadableDatabase().close();
		dbEvent.close();
	}
	
	@Override
    public boolean onOptionsItemSelected(final MenuItem item) 
    {
    	switch (item.getItemId()) {
        case android.R.id.home:
          super.onBackPressed();
    	}
          return true;
    }
}
